package org.xpandaframework.base.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xpandaframework.base.curd.impl.BaseEntity;
import org.xpandaframework.base.curd.impl.OperationConstants;
import org.xpandaframework.base.exception.XpandaFrameworkException;
import org.xpandaframework.base.service.BaseService;
import org.xpandaframework.base.utils.SessionUser;
import org.xpandaframework.base.utils.UUIDUtils;

/**
 * <p>
 * CopyRright (c)2008-2014: Camelot
 * <p>
 * Project: xpandaframework
 * <p>
 * Module ID: <模块类编号可以引用系统设计中的类编号>
 * <p>
 * Comments: 基础控制类
 * <p>
 * JDK version used: JDK1.7
 * <p>
 * NameSpace: org.xpandaframework.base.controller.BaseController.java
 * <p>
 * Author: Angus
 * <p>
 * Create Date: 2014年11月25日
 * <p>
 * Modified By: <修改人中文名或拼音缩写>
 * <p>
 * Modified Date: <修改日期>
 * <p>
 * Why & What is modified: <修改原因描述>
 * <p>
 * Version: v1.0
 */
public abstract class BaseController<T extends BaseEntity> {

  /**
   * @Fields logger : 获得日志服务
   */
  protected Logger logger = LoggerFactory.getLogger(BaseController.class);

  /**
   * 获取基础服务
   *
   * @return BaseService<T>
   */
  protected abstract BaseService<T> getBaseService();

  /**
   * 添加单条数据
   *
   * @param t
   * @return ControllerResult
   */
  @ResponseBody
  @RequestMapping(value = "/addOne", method = RequestMethod.POST)
  public ControllerResult addOne(T t) {
    final T o = setBaseEntityValues(t);
    final ControllerResult r = new ControllerResult();
    try {
      getBaseService().create(o);
      r.addDefultFail(o);
    } catch (final XpandaFrameworkException e) {
      r.addDefultSuccess(o);
      e.printStackTrace();
    }
    return r;
  }

  /**
   * 根据ID修改一条数据
   *
   * @param t
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "/editOneById", method = RequestMethod.POST)
  public ControllerResult editOneById(T t) {
    // final T o = setBaseEntityValues(t);
    final ControllerResult r = new ControllerResult();
    try {
      getBaseService().updateById(t);
      r.addDefultFail(t);
    } catch (final XpandaFrameworkException e) {
      r.addDefultSuccess(t);
      e.printStackTrace();
    }
    return r;
  }

  /**
   * 根据ID查询一条数据
   *
   * @param id
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "/searchOneById", method = RequestMethod.POST)
  public ControllerResult searchOneById(String id) {
    final ControllerResult r = new ControllerResult();
    try {
      r.addDefultFail(getBaseService().readOneById(id));
    } catch (final XpandaFrameworkException e) {
      e.printStackTrace();
    }
    return r;
  }

  /**
   * 根据ID删除一条数据
   *
   * @param id
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "/delOneById", method = RequestMethod.POST)
  public ControllerResult delOneById(String id) {
    final ControllerResult r = new ControllerResult();
    try {
      getBaseService().delOneById(id);
    } catch (final XpandaFrameworkException e) {
      e.printStackTrace();
    }
    return r;
  }

  /**
   * 给基础实体类赋值
   *
   * @param t
   *          将要被保存的实体类
   * @return T
   */
  public T setBaseEntityValues(T t) {
    final SessionUser u = getSessionUser();
    // 赋值ID
    t.setId(generateId());
    // 赋值创建时间
    t.setCreateTime(new Date());
    // 赋值创建人
    t.setCreator(u.getLoginID());
    // 赋值操作时间
    t.setOperationTime(new Date());
    // 赋值操作人
    t.setOperator(u.getLoginID());
    // 赋值操作标志
    t.setOperationFlag(OperationConstants.C);
    return t;
  }

  /**
   * 生成ID
   *
   * @return String
   */
  protected String generateId() {
    return UUIDUtils.create();
  }

  /**
   * 获取当前系统登陆用户
   *
   * @return SessionUser
   */
  public SessionUser getSessionUser() {
    final SessionUser u = new SessionUser();
    u.setLoginID("admin");
    u.setUserName("超级管理员");
    return u;
  }

}
